<template>
  <div class="cart">
    <u-head :title="tit"></u-head>
    <div class="cart-nav">
      <ul class="navigation">
        <li>全部(20)</li>
        <li>降价(4)</li>
        <li>常卖(0)</li>
        <li>分类</li>
      </ul>
      <div class="ziying">
        小U自营
        <div class="right">
          <span>以免运费 |</span>
          <span>领券</span>
        </div>
      </div>
    </div>
    <div class="cart-body">
      <div class="main" v-for="(item, index) in cartList" :key="item.id">
        <input :checked='item.checked' @click="select(index)" type="checkbox" class="inp" />
        <div class="right">
          <div class="image">
            <img :src="$pregImg + item.img" alt="" />
          </div>
          <div class="zhanshi">
            <p>{{ item.goodsname }}</p>
            <p>规格：不错的商品</p>
            <p>比加入时降低1000</p>
            <p>
              <span>￥{{ item.price }}</span
              ><span>￥{{ item.price + 2000 }}</span>
            </p>
          </div>
          <div class="jiajian">
            <i @click="item.num > 0 ? item.num-- : 0">-</i>
            <span>{{ item.num }}</span>
            <i @click="item.num++">+</i>
          </div>
          <div @click="del(index)" class="del">删除</div>
        </div>
      </div>
    </div>

    <div class="pay">
      <input ref="all" @click="checkAll" type="checkbox" class="inps" />
      <div class="heji">
        <p>
          合计：<span>￥{{ priceWatch }}</span>
        </p>
        <p>不含运费，已优惠</p>
        <p>￥0.00</p>
      </div>
      <div class="btn">
        <button @click="goPay">去结算({{ numWatch }}件)</button>
      </div>
    </div>
    <u-nav></u-nav>
  </div>
</template>
<script>
import uHead from "../components/public/uHead.vue";
import uNav from "../components/public/uNav.vue";
import { mapActions,mapGetters } from "vuex";
import http from "../utils/http";
import { getCartsApi } from '@/api/cart/cart'
export default {
  components: {
    uHead,
    uNav,
  },
  data() {
    return {
      tit: "购物车",
      cartList: [],
      totalPrice: 0,
    };
  },
  created() {
    /* console.log(this.$store.state.carts); */
    this.a();
    this.getList();
    this.getCartList();
    /* console.log(this.$store.state.carts.cartList); */
  },
  computed: {
    //查看总数
    numWatch() {
      let a = 0;
      this.cartList.forEach((item) => {
        if (item.checked) {
          a += item.num;
        }
      });
      return a;
    },
    //查看总价
    priceWatch() {
      let a = 0;
      this.cartList.forEach((item) => {
        if (item.checked) {
          a += item.num * item.price;
        }
      });
      return a;
    },
    ...mapGetters("carts", ["getPay"]),
  },
  methods: {
    //去支付
    goPay() {
      /* this.$store.state.carts.cartList */
      console.log(this.cartList);
      console.log(this.getPay);
      let temp=JSON.stringify(this.getPay)
      this.$router.push({ name:'order',query:{ order:temp } })
    },
    //全选
    checkAll(){
       this.cartList.forEach(item=>{
        item.checked=this.$refs.all.checked
       })
    },
    del(index) {
      this.cartList.splice(index, 1);
      let flag=true
      this.cartList.forEach(item=>{
        if(item.checked==false){
          flag=false
          return
        }
      })
      this.$refs.all.checked=flag
    },
    //是否选择
    select(index) {
      this.cartList[index].checked = !this.cartList[index].checked;
      let flag=true
      this.cartList.forEach(item=>{
        if(item.checked==false){
          flag=false
          return
        }
      })
      this.$refs.all.checked=flag
    },
    ...mapActions("carts", ["getList"]),
    //获取购物车列表
    getCartList() {
      if (this.$store.state.carts.cartList.length < 1) {
        this.cartList.push = JSON.parse(localStorage.getItem("arr"));
      }
      setTimeout(() => {
        this.cartList = this.$store.state.carts.cartList;
      }, 200);
      /* console.log(this.cartList); */
    },
    //解决页面刷下vuex数据短暂消失
    a() {
      let uid = JSON.parse(localStorage.getItem("user")).uid;
      http.get("/api/cartlist", { params: { uid } }).then((res) => {
        localStorage.setItem("arr", JSON.stringify(res.list));
      });
    },
    //实现购物车的增加和减少
    async getCarts(id,type){
      let obj={id,type}
      let res= await getCartsApi(obj)
      console.log(res.list);
    }
  },
};
</script>
<style lang="less" scoped>
.cart {
  max-width: 7.5rem;
}
.navigation {
  width: 7.5rem;
  height: 0.7rem;
  display: flex;
  justify-content: space-around;
  align-items: center;
  font-size: 0.3rem;
  color: #fff3f2;
  text-align: center;
  background: #ccc;
}
.fenlei {
  display: flex;
}
.icon-fenlei {
  font-size: 0.4rem;
}
.inp {
  width: 20px;
  height: 30px;
  margin-top: 50px;
}
.inpq {
  width: 20px;
  height: 30px;
  margin-top: 10px;
}
.inps {
  width: 20px;
  height: 30px;
  margin-top: 20px;
}
.cart {
  width: 100vw;
  height: auto;
  margin-bottom: 2.7rem;
  overflow: hidden;
  // background-color: #f2f3f2;

  .cart-nav {
    z-index: 100;
    width: 100%;
    height: 0.7rem;
    line-height: 0.7rem;
    background: linear-gradient(#ff6344, #ff897f);
    ul {
      display: flex;
      height: 100%;

      justify-content: space-around;
      li {
        font-size: 0.24rem;
        color: #fff;
      }
    }
    .ziying {
      display: flex;
      justify-content: space-between;
      padding: 0 0.3rem;
      width: 90%;
      height: 0.8rem;
      background-color: #fff;
      font-size: 0.27rem;
      .right {
        padding-top: 0.034rem;
        font-size: 0.27rem;
        span:last-child {
          color: #ff6040;
          margin-left: 0.08rem;
        }
      }
    }
  }
  .cart-body {
    margin-top: 1.7rem;

    width: 100%;

    .main {
      display: flex;
      position: relative;
      padding: 0 0.3rem;
      height: 2.5rem;
      background-color: #fff;
      margin-top: 0.3rem;

      .van-checkbox {
        width: 0.5rem;
      }
      .right {
        flex: 1;
        display: flex;
        align-items: center;
        height: 100%;
        position: relative;
        .image {
          text-align: center;
          width: 2.5rem;
          height: 2.5rem;
          img {
            width: 2.3rem;
            height: 2.3rem;
          }
        }
        .zhanshi {
          p {
            font-size: 0.24rem;
            margin-top: 0.1rem;
          }
          p:first-child {
            font-size: 0.25rem;
          }
          p:nth-child(2) {
            font-size: 0.24rem;
            color: #d2d2d2;
          }
          p:nth-child(3) {
            color: #ff6040;
          }
          p:last-child {
            span:first-child {
              font-size: 0.32rem;
              color: #ff6040;
              vertical-align: middle;
            }
            span:last-child {
              text-decoration: line-through;
              font-size: 0.22rem;
              color: #d2d2d2;
              vertical-align: middle;
            }
          }
        }
        .jiajian {
          position: absolute;
          top: 65%;
          right: 0.23rem;
          padding: 0 0.06rem;
          font-size: 0.4rem;
          i:first-child {
            display: inline-block;
            width: 0.2rem;
            background-color: #000;
            height: 0.035rem;
            margin-top: 0.27rem;
            margin-right: 0.06rem;
          }
          span {
            display: inline-block;
            padding: 0 0.27rem;
            height: 0.5rem;
            line-height: 0.5rem;
            // padding-top: .06rem;
            font-size: 0.34rem;
            margin: 0 0.06rem;
            text-align: center;
            background-color: #f2f3f2;
          }
          i:last-child {
            margin-top: 0.06rem;
            display: inline-block;
          }
        }
        .del {
          position: absolute;
          font-size: 12px;
          top: 10px;
          right: 10px;
          width: 40px;
          height: 20px;
          line-height: 20px;
          color: #fff;
          background-color: #ff6344;
          text-align: center;
          border-radius: 4px;
        }
      }
    }
  }
  .pay {
    position: fixed;
    display: flex;
    bottom: 1rem;
    width: 100%;
    height: 1.1rem;
    padding: 0 0.3rem;
    padding-right: 0;
    border-bottom: 1px solid #ccc;
    background-color: #fff;
    .van-checkbox {
      font-size: 0.24rem;
    }
    .heji {
      width: 3.5rem;
      height: 100%;
      padding-left: 0.7rem;
      padding-top: 0.12rem;
      p {
        font-size: 0.2rem;
        color: #b9b9b9;
      }
      p:first-child {
        color: #000;
        font-size: 0.24rem;
        span {
          font-style: 0.24rem;
          color: #ff603f;
        }
      }
    }
    .btn {
      flex: 1;
      button {
        width: 80%;
        height: 100%;
        color: #fff;
        font-size: 0.3rem;
        background-color: #ff603f;
      }
    }
  }
}
</style>
